﻿using Acme.BookStore.AcmeModel;
using Acme.BookStore.Pdepat;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    /// <summary>
    /// 部门
    /// </summary>
    public class PdepatServer :
        CrudAppService<
            PdepatInfo, //The Book entity
            PdepatInfoDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            PdepatInfos>, //Used to create/update a book
        IPdepatInfo //implement the IBookAppService
    {
        public IRepository<PdepatInfo, Guid> _pdepa;
        public IRepository<SuperiInfo, Guid> _super;
        public PdepatServer(IRepository<PdepatInfo, Guid> pdepa, IRepository<SuperiInfo, Guid> super)
            : base(pdepa)
        {
            _pdepa = pdepa;
            _super = super;
        }
        //显示
        public List<PdepatInfos> GetShow()
        {
            var str = (from a in _pdepa
                       join b in _super
                       on a.DepartmentLevel equals b.Id.ToString()
                       select new PdepatInfos
                       {
                           DepartmentName = a.DepartmentName,
                           DepartmentTime = a.DepartmentTime,
                           SuperiorName = b.SuperiorName,
                       }).ToList();
            return str;
        }
        [HttpPost]
        //添加
        public async Task<int> GetAdd(PdepatInfos m)
        {
            PdepatInfo str = ObjectMapper.Map<PdepatInfos, PdepatInfo>(m);
            await _pdepa.InsertAsync(str);
            return 1;
        }
        //删除
        public async Task<int> GetDel(Guid Id)
        {
            await _pdepa.DeleteAsync(Id);
            return 1;
        }
        //反填
        public async Task<PdepatInfoDto> GetFt(Guid Id)
        {
            PdepatInfo str = await _pdepa.GetAsync(Id);
            return ObjectMapper.Map<PdepatInfo, PdepatInfoDto>(str);
        }
        //修改
        public async Task<int> GetUpd(PdepatInfoDto m)
        {
            PdepatInfo str = await _pdepa.GetAsync(m.Id);
            SuperiInfo trs = await _super.GetAsync(m.Id);
            str.DepartmentName = m.DepartmentName;
            str.DepartmentLevel = m.DepartmentLevel;
            str.DepartmentTime = m.DepartmentTime;
            trs.SuperiorName = m.SuperiorName;
            return 1;
        }
    }
}
